<template>
  <el-dialog
    :title="{ add: '新增用户', edit: '编辑用户' }[mode]"
    center
    :visible.sync="show"
    width="40%"
    @close="closeEvent"
  >
    <el-form
      :model="form"
      label-width="80px"
      :rules="rules"
      style="width-400px"
      ref="form"
    >
      <el-form-item prop="username" label="用户名">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item prop="email" label="邮箱">
        <el-input v-model="form.email" />
      </el-form-item>
      <el-form-item prop="phone" label="电话">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item prop="role_id" label="角色">
        <el-select v-model="form.role_id" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            formatter="role"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="status" label="状态">
        <el-select v-model="form.status" placeholder="请选择">
          <el-option
            v-for="item in option"
            :key="item.status"
            :label="item.label"
            :value="item.status"
            formatter="status"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="remark" label="用户备注">
        <el-input v-model="form.remark" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="show = false">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </template>
  </el-dialog>
</template>
<script>
import { useradd, userredit } from '@/api/userlist'
export default {
  data () {
    return {
      show: false,
      mode: '',
      form: {
        username: '',
        email: '',
        phone: '',
        role_id: '',
        status: '',
        remark: ''
      },
      options: [
        {
          value: 1,
          label: '超级管理员'
        },
        {
          value: 2,
          label: '管理员'
        },
        {
          value: 3,
          label: '老师'
        },
        {
          value: 4,
          label: '学生'
        }
      ],
      option: [
        {
          status: 1,
          label: '启用'
        },
        {
          status: 0,
          label: '禁用'
        }
      ],
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'change' }
        ],
        email: [
          {
            required: true,
            message: '请输入邮箱',
            trigger: 'change'
          },
          {
            pattern:
              /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/,
            message: '请输入正确的邮箱',
            trigger: 'change'
          }
        ],
        phone: [
          {
            required: true,
            message: '请输入电话',
            trigger: 'change'
          },
          {
            pattern: '^1[3-9]\\d{9}$',
            message: '请输入正确的电话',
            trigger: 'change'
          }
        ]
        // role_id: [{ required: true, message: '请选择角色', trigger: 'change' }],
        // status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      },
      userForm: {
        username: '',
        email: '',
        phone: '',
        role_id: '',
        status: ''
      }
    }
  },
  methods: {
    // closeEvent () {
    //   this.showTree = false
    //   this.$refs.form.resetFields()
    // },
    // outEvent () {
    //   this.showTree = false
    // },
    // 新增
    submit () {
      this.$refs.form.validate(async (result) => {
        if (result) {
          if (this.mode === 'add') {
            const res = await useradd(this.form)
            console.log(res, '11')
            if (res.code === 200) {
              this.$message.success('添加成功')
              this.show = false
            } else {
              this.$message.error(res.message)
            }
          } else if (this.mode === 'edit') {
            await userredit(this.form)
            this.$message.success('编辑成功')
            this.show = false
          }
          this.$emit('getData')
        }
      })
    },
    closeEvent () {
      // 清空表单数据
      this.showTree = false
      this.$refs.form.resetFields()
      this.form = {
        username: '',
        email: '',
        phone: '',
        role_id: '',
        status: ''
      }
    },
    role (row, column, cellValue, index) {
      if (row.role_id === 1) {
        return '超级管理员'
      } else if (row.role_id === 2) {
        return '管理员'
      } else if (row.role_id === 3) {
        return '老师'
      } else if (row.role_id === 4) {
        return '学生'
      } else {
        return '-'
      }
    },
    status (row, column, cellValue, index) {
      if (row.status === 1) {
        return '启用'
      } else if (row.status === 0) {
        return '禁用'
      } else {
        return '-'
      }
    }
    // 确定点击
    // submit () {
    //   this.$refs.form.validate(async result => {
    //     console.log(result)
    //     if (result) {
    //       await useradd(this.form)
    //       this.$message.success('新增员工成功')
    //       this.show = false
    //       this.$emit('getData')
    //     }
    //   })
    // }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__header {
  background-color: #109dfa;
}
</style>
